package de.flohrit.mt4j.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SmtpEmailSender {
	/**
	 * send smtp email.<br>
	 * Example: <blockquote>
	 * 
	 * <pre>
	 * SmtpEmailSender email = new SmtpEmailSender();
	 * String smtpServerAddress = "smtp.gmail.com";
	 * String originEmailAddress ="xxx.yy@xxx.com.sg";
	 * String destinationEmailAddress = "zz.zz@xxx.com.sg";
	 * String password = "123456";
	 * String title = "Subject";
	 * String content = "content";
	 * email.sendMail(smtpServerAddress, originEmailAddress, destinationEmailAddress, password, title, content);
	 *  </blockquote>
	 * </pre>
	 * 
	 * @param smtpServerAddress
	 * @param originEmailAddress
	 * @param destinationEmailAddress
	 * @param password
	 * @param title
	 * @param content
	 * @return <code>done</code> if email sent successfully,
	 *         <code>throw RuntimeException</code> otherwise
	 */
	public boolean sendGmail(String smtpServerAddress, final String originEmailAddress, String destinationEmailAddress, final String password, String title, String content) {
		Properties props = new Properties();
		props.put("mail.smtp.host", smtpServerAddress);
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(originEmailAddress, password);
			}
		});

		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(originEmailAddress));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinationEmailAddress));
			message.setSubject(title);
			message.setText(content);
			Transport.send(message);
			return true;

		} catch (MessagingException e) {
			System.out.println(e);
			// if program reaches here, that means email sending fail,
			// false will be return later, so no exception shall be thrown
		}
		return false;
	}

//	public static void main(String[] args) {
//		SmtpEmailSender email = new SmtpEmailSender();
//		String smtpServerAddress = "smtp.gmail.com";
//		String originEmailAddress = "limk0032@gmail.com";
//		String destinationEmailAddress = "limk0032@gmail.com";
//		String password = "eatmhkdukiychdpg";
//		String title = "Subject";
//		String content = "content";
//		email.sendGmail(smtpServerAddress, originEmailAddress, destinationEmailAddress, password, title, content);
//	}

	public static void send(final String title, final String content) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				SmtpEmailSender email = new SmtpEmailSender();
				String smtpServerAddress = "smtp.gmail.com";
				String originEmailAddress = "limk0032@gmail.com";
				String destinationEmailAddress = "limk0032@gmail.com";
				String password = "eatmhkdukiychdpg";
				email.sendGmail(smtpServerAddress, originEmailAddress, destinationEmailAddress, password, title, content);
			}
		}).run();
	}
}
